package org.yscz.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.yscz.common.Constants;
import org.yscz.common.HttpStatus;
import org.yscz.common.Result;
import org.yscz.entity.RoomBean;
import org.yscz.media.MediaService;
import org.yscz.mqtt.MqttCustomClient;
import org.yscz.mqtt.PubCallBack;
import org.yscz.service.EvaluationService;
import org.yscz.utils.DownloadImage;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 拍照上传接口
 *
 * @author achen
 * @create 2021-08-25 11:13
 */
@RestController
@Slf4j
@Api("拍照上传接口信息")
public class PicturesUploadController {

    @Autowired
    private EvaluationService evaluationService;

    @Autowired
    private MediaService mediaService;

    @Autowired
    private MqttCustomClient mqttCustomClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${yscz.mediaServer.ip}")
    private String mediaIp;

    @Value("${yscz.mediaServer.flv-port}")
    private Integer flvPort;

    @Value("${yscz.usbDevice.cameraUrl}")
    private String cameraUrl;

    @Value("${yscz.pictures.upload.location}")
    private String location;

    @Value("${yscz.pictures.upload.httpLocation}")
    private String httpLocation;

    @Value("${yscz.alg.uuid}")
    private String algUuid;

    //通知算法入人脸库的图片路径
    public static String imageKey = "";

    /**
     * 拍照上传对应的流，用于拍照上传成功后关闭流
     */
    public static String mediaUuid;

    @GetMapping("/pictures/mediaStart")
    @ApiOperation(value = "拍照上传视频流推送接口", httpMethod = "GET")
    public Result mediaStart() {
        String uuid = UUID.randomUUID().toString();
        mediaUuid = uuid;

        RoomBean roomBean = new RoomBean();
        roomBean.setUuid(uuid);
        roomBean.setCameraUrl(cameraUrl);

        //调用流媒体接口 开启流媒体流 false标识不启用算法
        evaluationService.startEvaluation(roomBean, false);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uuid", uuid);
        //rtsp流转换为.flv
        jsonObject.put("cameraUrl", FaceController.flvMethod(uuid, mediaIp, flvPort));
        return Result.success(HttpStatus.SUCCESS, "视频流推送成功", jsonObject);
    }

    @PostMapping("/pictures/picturesUpload")
    @ApiOperation(value = "拍照上传接口", httpMethod = "GET")
    public Result picturesUpload(@RequestBody JSONObject jsonObjectVo) {
        log.info("jsonObjectVo:[{}]", jsonObjectVo);
        String uuid = (String) jsonObjectVo.get("uuid");
        //role：0-测评人 1-被测人
        Integer type = (Integer) jsonObjectVo.get("type");
        log.info("拍照上传type:[{}]", type);

        //Map<String, Object> map = new HashMap<>();

        //调用流媒体拍照接口
        //http://192.168.16.125:9090/getImage?src=rtsp://192.168.16.125:8554/live/278b9632-dcc3-41bd-9a21-afaa449ac79a
        String imageUrl = mediaService.getImage(uuid);
        log.info("imageUrl:[{}]", imageUrl);

        //上传接收的流媒体传递的图片数据
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(uuid);
        stringBuilder.append(".jpg");
        log.info("图片名为:[{}]", stringBuilder.toString());
        //拍照上传
        DownloadImage.download(imageUrl, stringBuilder.toString(), location);

        //通知算法uuid 路径 C:\Users\achen\Desktop\pe\4e6b4ce0-75cd-4352-a1e5-ff1bc597c060.jpg
        String imageLocation = location + stringBuilder.toString();
        log.info("imageLocation:[{}]", imageLocation);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uid", algUuid);
        jsonObject.put("image_url", imageLocation);
        jsonObject.put("role", type);
        String imageJson = JSONObject.toJSONString(jsonObject);
        log.info("拍照上传imageJson:[{}]", imageJson);

        imageKey = stringBuilder.toString();
        //根据图片名保存通知算法的信息
        stringRedisTemplate.opsForValue().set(imageKey, imageJson);

        //发布信息给算法
        mqttCustomClient.publish(Constants.IMAGE_QUALITY_CONTROL, imageJson);

        jsonObject.put("image_url", httpLocation + stringBuilder.toString());
        return Result.success(HttpStatus.SUCCESS, "图片上传成功", jsonObject);
    }
}
